<?php
/**
 * Copyright (c) 2006, PORTALIER Julien
 *
 * Licensed under The LGPL License
 * Redistributions of files must retain the above copyright notice.
 *
 * @package      FeatherCMS
 * @subpackage   Members
 * @copyright    Copyright (c) 2006, Julien PORTALIER
 * @link         http://julien.portalier.com
 * @license      http://www.opensource.org/licenses/lgpl-license.php The LGPL License
 */

class Group extends Model
{
	protected $belongsTo = array('Member');
	
	public $recursiveProcess = Q_PROCESS_LIST;
	public $recursiveFields  = 'name';
	
	function beforeValidate()
	{
		if (empty($this->validate))
		{
			$this->validate = array(
				'member_id' => array(VALID_NOT_EMPTY => ''),
				'name'      => array(VALID_NOT_EMPTY => ''),
			);
		}
		return true;
	}
	
	function afterValidate()
	{
		// is already member of the group?
		$this->conditions(array(
			'member_id' => $this->data['Group']['member_id'],
			'name'      => $this->data['Group']['name']
		));
		$find = $this->field('id', null, -1);
		return empty($find);
	}
	
	function register($member_id, $group)
	{
		return $this->save(array(
			'member_id' => $member_id,
			'name'      => $group
		));
	}
}
?>